﻿using RYB_PTL_API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class BLightElecLabel
    {
        //RYB_PTL.UserResultAvailableEventHandler +=new RYB_PTL.UserResultAvailableEventHandler(RYB_PTL_UserResultAvailable);

        private string RYB_PTL_UserResultAvailable(RYB_PTL.RtnValueStruct rs)
        {
            try
            {
                string sIP = rs.Ip;
                string sTagID = rs.Tagid;
                string sValue = rs.Number;
                string sKeyCode = rs.KeyCode;
                string sLocator = rs.Locator;
                string sCombineStrToRichTextBox = string.Format("IP地址：{0}，标签ID：{1}，货位：{2}，特征值：{3}，返回数值：{4}"
                    , sIP, sTagID, sLocator, sKeyCode, sValue);
                return sCombineStrToRichTextBox;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 根据控制器IP和端口连接控制器
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="iPort"></param>
        /// <returns></returns>
        public static bool ElecLabelControler_Connect(string sIP, int iPort)
        {
            return RYB_PTL.RYB_PTL_Connect(sIP, iPort);
        }

        /// <summary>
        /// 主程序退出时断开所有控制器连接
        /// </summary>
        /// <returns></returns>
        public static bool ElecLabelController_Disconnect()
        {
            return RYB_PTL.RYB_PTL_Disconnect();
        }

        /// <summary>
        /// 断开指定IP地址的控制器
        /// </summary>
        /// <param name="sIP"></param>
        /// <returns></returns>
        public static bool ElecLabelController_Disconnect(string sIP)
        {
            return RYB_PTL.RYB_PTL_Disconnect(sIP);
        }

        /// <summary>
        /// 获取指定IP地址的控制器连接状态
        /// </summary>
        /// <param name="sIP"></param>
        /// <returns></returns>
        public static bool ElecLabelController_ConnectState(string sIP)
        {
            return RYB_PTL.RYB_PTL_GetConnectState(sIP);
        }

        /// <summary>
        /// 点亮五位电子标签--仅数量
        /// </summary>
        /// <param name="sIP">控制器IP</param>
        /// <param name="sLabelID">标签号</param>
        /// <param name="iNumber">显示商品数</param>
        /// <param name="iMode">亮灯模式1：静态显示2：闪烁显示1-7闪烁频率增加</param>
        /// <param name="iColor">1-7其中颜色（红、绿、蓝、黄、粉、浅蓝、白）</param>
        /// <param name="iUpdown">大灯能否被拍灭1：数量不可调不可拍灭2：数量不可调大灯可拍灭，此参数不传输可以增减，可拍灭</param>
        /// <returns></returns>
        public static bool DisplayElecLabel(string sIP, string sLabelID, int iNumber, int iMode=1, int iColor=1 ,int iUpdown=2)
        {

            //bool isCon = RYB_PTL_API.RYB_PTL.RYB_PTL_Connect(sIP, 6020);
            bool isCon = RYB_PTL_API.RYB_PTL.RYB_PTL_GetConnectState(sIP);
            //if (isCon)
            //{
                
            //}
            //else
            //{
            //    //查看运行目录下log文件下的日志记录错误信息。
            //}
            return RYB_PTL.RYB_PTL_DspDigit5(sIP, sLabelID.PadLeft(4, '0'), iNumber, iMode, iColor);
            //return RYB_PTL.RYB_PTL_DspDigit5(sIP, sLabelID, iNumber, iMode, iColor,iUpdown);
        }

        /// <summary>
        /// 熄灭电子标签（也可手动拍灭）
        /// </summary>
        /// <param name="sIP"></param>
        /// <param name="sLabelID"></param>
        /// <returns></returns>
        public static bool CloseElecLabel(string sIP, string sLabelID)
        {
            return RYB_PTL.RYB_PTL_CloseDigit5(sIP, sLabelID);
        }

        /// <summary>
        /// 巷道灯控制
        /// </summary>
        /// <param name="sIP">控制器IP</param>
        /// <param name="sLabelID">标签ID</param>
        /// <param name="iPort">亮端口为5、6、7的巷道灯（三色灯）</param>
        /// <param name="iMode">0：熄灭1：慢闪2：中闪3：快闪</param>
        /// <returns></returns>
        public static bool RoadwayLightControl(string sIP, string sLabelID, int iPort, int iMode)
        {
            return RYB_PTL.RYB_PTL_AisleLamp(sIP, sLabelID, iPort, iMode);
        }

        /// <summary>
        /// 获取控制器缓冲池内拍灯回执信息
        /// </summary>
        /// <returns></returns>
        public static object GetControlerCatchPool()
        {
            return RYB_PTL.RYB_PTL_GetDigit("", "");
        }
        

    }
}
